package ca.seia.comp6521.a1.memory;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;

import org.junit.Test;

public class InputMemoryBufferTestCase {

	@Test
	public void testCreatingAnInputBuffer() {
		
		String assignedSublistFile = "C:/comp6521_files/phase1_sublist1.txt";
		int capacity = 4; // 4 integers.
		InputMemoryBuffer iBuffer = null;
		try {
			iBuffer = new InputMemoryBuffer(assignedSublistFile, capacity);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			fail();
		}
		
		assertEquals(assignedSublistFile, iBuffer.getAssignedSublistFile());
		assertEquals(capacity, iBuffer.getCapacity());
		
	}
	
	@Test 
	public void testInsertionIntoBuffer() {
		
		String assignedSublistFile = "C:/comp6521_files/phase1_sublist1.txt";
		int capacity = 4; // 4 integers.
		InputMemoryBuffer iBuffer =null;
		try {
			iBuffer = new InputMemoryBuffer(assignedSublistFile, capacity);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			fail();
		}
		
		try {
			iBuffer.Insert(1);
			iBuffer.Insert(2);
			iBuffer.Insert(3);
			iBuffer.Insert(4);
			assertEquals(4, iBuffer.getSize());
		
		} catch (FullBufferException e) {
			fail();
		}			
		
		try {
			iBuffer.Insert(5);
			fail("No Exception is thrown for Going Over Capacity");
		
		} catch (FullBufferException e) {
			assertEquals("Cannot Insert Record. Buffer is Full.", e.getMessage());
		}			
	
	}
	
	@Test 
	public void testPeek() {
	
		String assignedSublistFile = "C:/comp6521_files/phase1_sublist1.txt";
		int capacity = 4; // 4 integers.
		InputMemoryBuffer iBuffer = null;
		try {
			iBuffer = new InputMemoryBuffer(assignedSublistFile, capacity);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			iBuffer.Insert(1);
			iBuffer.Insert(2);
			iBuffer.Insert(3);
			iBuffer.Insert(4);
			assertEquals(4, iBuffer.getSize());
		
		} catch (FullBufferException e) {
			fail();
		}
		
		assertEquals(1, iBuffer.peek());
		assertEquals(4, iBuffer.getSize());
	}
	
	
	@Test 
	public void testPoll() {
	
		String assignedSublistFile = "C:/comp6521_files/phase1_sublist1.txt";
		int capacity = 4; // 4 integers.
		InputMemoryBuffer iBuffer = null;
		
		try {
			iBuffer = new InputMemoryBuffer(assignedSublistFile, capacity);
			iBuffer.Insert(1);
			iBuffer.Insert(2);
			iBuffer.Insert(3);
			iBuffer.Insert(4);
			assertEquals(4, iBuffer.getSize());
		
		} catch (FullBufferException | FileNotFoundException e) {
			fail();
		}
		
		int polledInt = iBuffer.poll();
		assertEquals(1, polledInt);
		assertEquals(3, iBuffer.getSize());
		assertEquals(2, iBuffer.peek());
	}
}
